/*
 * Copyright (C) 2012 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.codeminer.parts;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.codeminer.database.DatabaseConnection;
import org.codeminer.database.DatabaseException;
import org.codeminer.database.DatabaseManager;
import org.codeminer.parts.application.ApplicationComponent;
import org.codeminer.parts.application.ApplicationContext;
import org.codeminer.parts.application.ShutdownCallback;
import org.codeminer.parts.database.UserConnector;
import org.codeminer.parts.server.users.User;

/**
 * TestMock for DatabaseManager.
 *
 * @author Daniel Wasilew <daniel.wasilew@codeminer.org>
 */
public class DummyDatabaseManager extends DatabaseManager implements ApplicationComponent {

    private static long CURRENT_ID = 0;

    public DummyDatabaseManager() {
        registerConnector(new DummyUserConnector());
    }

    @Override
    public void clearDatabase() throws DatabaseException {
        //nothing to do
    }

    @Override
    protected boolean databaseExists() throws DatabaseException {
        return true;
    }

    @Override
    public int getCurrentDatabaseVersion() {
        return 1;
    }

    @Override
    protected DatabaseConnection getDatabaseConnection() throws DatabaseException {
        return new DatabaseConnection(null);
    }

    @Override
    public int getSupportedDatabaseVersion() {
        return 1;
    }

    @Override
    protected void initializeDatabase() throws DatabaseException {
    }

    @Override
    protected void loadDatabase() throws DatabaseException {
    }

    @Override
    protected void shutdownDatabase() throws DatabaseException {
    }

    @Override
    public void startup(ApplicationContext context, ShutdownCallback shutdownCallback) throws Exception {
        //nothing to do here
    }

    @Override
    public void shutdown() {
        //nothing to do here
    }

    @Override
    public Class getDescriptionClass() {
        return DatabaseManager.class;
    }

    private final static class DummyUserConnector implements UserConnector {

        private final Map<String, User> users = new HashMap<>();
        private final Map<String, String> userPasswords = new HashMap<>();

        @Override
        public boolean deleteUser(User user) throws DatabaseException {
            return this.users.remove(user.getName()) != null;
        }

        @Override
        public User getUser(String name) throws DatabaseException {
            return this.users.get(name);
        }

        @Override
        public String getUserPassword(User user) throws DatabaseException {
            return this.userPasswords.get(user.getName());
        }

        @Override
        public User storeUser(User user, String password) throws DatabaseException {
            this.users.put(user.getName(), user);
            this.userPasswords.put(user.getName(), password);
            return user;
        }

        @Override
        public boolean updateUser(User user, String newPassword) throws DatabaseException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public List<User> getUsers() throws DatabaseException {
            return new LinkedList<>(this.users.values());
        }

        @Override
        public Class getAccessorDescription() {
            return UserConnector.class;
        }

        @Override
        public void setDatabaseConnection(DatabaseConnection databaseConnection) {
            //nothing to do here
        }
    }
}
